<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <button>获取数据</button>
    <ul></ul>

    <script>
        let button = document.querySelector('button');
        let ul = document.querySelector('ul');

        button.onclick = function() {
            let source = new EventSource("/");

            source.addEventListener('ping', function(e) {
                console.log('e', e);
                let data = JSON.parse(e.data);
                
                ul.innerHTML = '';

                data.forEach(d => {
                    let li = document.createElement('li');
                    li.innerHTML = d.name;
                    ul.appendChild(li);
                })
            });
        }
    </script>
</body>
</html>